rgba: Use pango_color_parse_with_alpha
authorMatthias Clasen <mclasen@redhat.com>
Mon, 3 Aug 2020 03:23:49 +0000 (23:23 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 3 Aug 2020 03:30:33 +0000 (23:30 -0400)
Use this newly exported pango function, so we
can support hex colors with alpha like #rrggbbaa.

Fixes: #2931
gdk/gdkrgba.c
testsuite/gdk/rgba.c

index 20940db9d33c28455f7d14abcdfba53f5d2570b9..fac9d2efa54c8a89141f75945e93ce13154b9058 100644 (file)
@@ -170,19 +170,21 @@ parse_rgb_value (const char   *str,
  * - A standard name (Taken from the X11 rgb.txt file).
  * - A hexadecimal value in the form “\#rgb”, “\#rrggbb”,
  *   “\#rrrgggbbb” or ”\#rrrrggggbbbb”
+ * - A hexadecimal value in the form “\#rgba”, “\#rrggbbaa”,
+ *   or ”\#rrrrggggbbbbaaaa”
  * - A RGB color in the form “rgb(r,g,b)” (In this case the color will
  *   have full opacity)
  * - A RGBA color in the form “rgba(r,g,b,a)”
  *
  * Where “r”, “g”, “b” and “a” are respectively the red, green, blue and
- * alpha color values. In the last two cases, “r”, “g”, and “b” are either integers
- * in the range 0 to 255 or percentage values in the range 0% to 100%, and
- * a is a floating point value in the range 0 to 1.
+ * alpha color values. In the last two cases, “r”, “g”, and “b” are either
+ * integers in the range 0 to 255 or percentage values in the range 0% to
+ * 100%, and a is a floating point value in the range 0 to 1.
  *
  * Returns: %TRUE if the parsing succeeded
  */
 gboolean
-gdk_rgba_parse (GdkRGBA     *rgba,
+gdk_rgba_parse (GdkRGBA    *rgba,
                 const char *spec)
 {
   gboolean has_alpha;
@@ -207,18 +209,19 @@ gdk_rgba_parse (GdkRGBA     *rgba,
   else
     {
       PangoColor pango_color;
+      guint16 alpha;
 
       /* Resort on PangoColor for rgb.txt color
        * map and '#' prefixed colors
        */
-      if (pango_color_parse (&pango_color, str))
+      if (pango_color_parse_with_alpha (&pango_color, &alpha, str))
         {
           if (rgba)
             {
               rgba->red = pango_color.red / 65535.;
               rgba->green = pango_color.green / 65535.;
               rgba->blue = pango_color.blue / 65535.;
-              rgba->alpha = 1;
+              rgba->alpha = alpha / 65535.;
             }
 
           return TRUE;
index db3fbd31117d05f51c9f9b498dc94042ee576ebf..26a94655e21b9afe591505d4f6e28ba6ffca4af1 100644 (file)
@@ -57,6 +57,14 @@ test_color_parse (void)
   res = gdk_rgba_parse (&color, "rgb(0,0,0)");
   g_assert (res);
   g_assert (gdk_rgba_equal (&color, &expected));
+
+  expected.red = 0.0;
+  expected.green = 0x8080 / 65535.;
+  expected.blue = 1.0;
+  expected.alpha = 0x8888 / 65535.;
+  res = gdk_rgba_parse (&color, "#0080ff88");
+  g_assert (res);
+  g_assert (gdk_rgba_equal (&color, &expected));
 }
 
 static void